﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QLChoDiaOc.Process;

namespace QLChoDiaOc.Forms
{
    /// <summary>
    /// Interaction logic for FormPhieuThu.xaml
    /// </summary>
    public partial class FormPhieuThu : Page
    {
        GetID _getID = new GetID();
        SelectInfo _selectInfo = new SelectInfo();
        InsertInfo _insertInfo = new InsertInfo();

        public FormPhieuThu()
        {
            InitializeComponent();
            HienThiThongTin();
        }

        private void HienThiThongTin()
        {
            App.PhieuThuTemp.MaPhieuThu = _getID.GenerateMaPhieuThu();
            App.PhieuThuTemp.NgayHetHan = DateTime.Parse(App.PhieuThuTemp.NgayThu.ToString()).AddMonths((int)_selectInfo.SelectThamSo_SoThangCoGiaTri());

            maPhieuThuTextBox.Text = App.PhieuThuTemp.MaPhieuThu;
            maDangKyTextBox.Text = App.PhieuThuTemp.MaDangKy;
            soTienThuTextBox.Text = App.PhieuThuTemp.SoTien.ToString();
            ngayThuDatePicker.SelectedDate = App.PhieuThuTemp.NgayThu;
            ngayHetHanDatePicker.SelectedDate = App.PhieuThuTemp.NgayHetHan;
            if (App.IsChapNhanQCBang)
                qcBangCheckBox.IsChecked = true;
            if (App.IsChapNhanQCBao)
                qcBaoCheckBox.IsChecked = true;
            if (App.IsChapNhanQCToBuom)
                qcToBuomCheckBox.IsChecked = true;
        }

        private void btnHoanTat_Click(object sender, RoutedEventArgs e)
        {
            //Insert Khach Hang
            try
            {
                if (App.IsKhachMoi == true && App.IsKhachCu == false)
                    _insertInfo.InsertKhachHang(App.KhachHangTemp.MaKH,
                        App.KhachHangTemp.HoTen,
                        DateTime.Parse(App.KhachHangTemp.NgaySinh.ToString()),
                        App.KhachHangTemp.DiaChi,
                        App.KhachHangTemp.Email,
                        App.KhachHangTemp.SoDT);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông Báo");
            }

            //Insert Dia Oc
            try
            {
                _insertInfo.InsertDiaOc(App.DiaOcTemp.MaDiaOc,
                    App.DiaOcTemp.MaKH,
                    App.DiaOcTemp.MaLoaiDiaOc,
                    App.DiaOcTemp.SoNha,
                    App.DiaOcTemp.Duong,
                    App.DiaOcTemp.Quan,
                    int.Parse(App.DiaOcTemp.DienTichDat.ToString()),
                    int.Parse(App.DiaOcTemp.DienTichSuDung.ToString()),
                    App.DiaOcTemp.ViTri,
                    App.DiaOcTemp.HuongNha,
                    App.DiaOcTemp.MoTa,
                    App.DiaOcTemp.GiaBan,
                    App.DiaOcTemp.TinhTrang);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông Báo");
            }

            //Insert Phieu Dang Ky
            try
            {
                if (App.IsChupAnh == true)
                    _insertInfo.InsertPhieuDangKy(App.PhieuDangKyTemp.MaDangKy,
                        App.PhieuDangKyTemp.MaDiaOc,
                        DateTime.Parse(App.PhieuDangKyTemp.NgayDangKy.ToString()),
                        DateTime.Parse(App.PhieuDangKyTemp.NgayHen.ToString()));
                else
                    _insertInfo.InsertPhieuDangKy(App.PhieuDangKyTemp.MaDangKy,
                        App.PhieuDangKyTemp.MaDiaOc,
                        DateTime.Parse(App.PhieuDangKyTemp.NgayDangKy.ToString()));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông Báo");
            }

            //Insert QC Bang
            try
            {
                if (App.IsChapNhanQCBang == true)
                    _insertInfo.InsertQCBang(App.QcBangTemp.MaQCBang,
                        App.QcBangTemp.MaDangKy,
                        App.QcBangTemp.MaLoaiBang,
                        int.Parse(App.QcBaoTemp.ThanhTien.ToString()),
                        int.Parse(App.QcBaoTemp.TinhTrang.ToString()));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông Báo");
            }

            //Insert QC Bao
            try
            {
                if (App.IsChapNhanQCBao == true)
                    _insertInfo.InsertQCBao(App.QcBaoTemp.MaQCBao,
                        App.QcBaoTemp.MaToBao,
                        App.QcBaoTemp.MaDangKy,
                        int.Parse(App.QcBaoTemp.CoHinhAnh.ToString()),
                        int.Parse(App.QcBaoTemp.ThanhTien.ToString()),
                        int.Parse(App.QcBaoTemp.TinhTrang.ToString()),
                        int.Parse(App.QcBaoTemp.SoKy.ToString()),
                        int.Parse(App.QcBaoTemp.KyBatDau.ToString()));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông Báo");
            }

            //Insert QC To Buom
            try
            {
                if (App.IsChapNhanQCToBuom == true)
                    _insertInfo.InsertQCToBuom(App.QcToBuomTemp.MaQCToBuom,
                        App.QcToBuomTemp.MaDangKy,
                        int.Parse(App.QcToBuomTemp.SoLuong.ToString()),
                        int.Parse(App.QcToBuomTemp.DonGia.ToString()),
                        int.Parse(App.QcToBuomTemp.ThanhTien.ToString()));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông Báo");
            }

            //Insert Phieu Thu
            try
            {
                _insertInfo.InsertPhieuThu(App.PhieuThuTemp.MaPhieuThu,
                    App.PhieuThuTemp.MaDangKy,
                    DateTime.Parse(App.PhieuThuTemp.NgayThu.ToString()),
                    int.Parse(App.PhieuThuTemp.SoTien.ToString()),
                    DateTime.Parse(App.PhieuThuTemp.NgayHetHan.ToString()));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông Báo");
            }

            App.Mainform.btnLapPhieuThu.IsEnabled = false;
            App.Mainform.btnXacNhanGiayTo.IsEnabled = false;
            App.Mainform.btnDangKyDichVu.IsEnabled = false;
            App.Mainform.btnDangKyDiaOc.IsEnabled = false;
        }
    }
}
